/**
 * Copyright (C), 2007-2009, 三五互联科技有限公司
 * File Name: CustomerValidator.js
 * Encoding: UTF-8
 * Version: 1.0
 * Date: Apr 23, 2008
 * History:	
 */

/**
 * 自定义的验证规则
 * 
 * @author 胡克雷(hukl424@163.com)
 * @version Revision: 1.00 Date: Apr 23, 2008
 */
CustomerValidator = new Object();

/**
 * 验证邮件地址格式是否正确
 * 
 * @param 需要验证的邮件地址
 * @return 布尔值
 */
CustomerValidator.isEmail = function(emailStr){
     if (emailStr.length == 0) {
         return true;
     }
     
     var emailPat=/^(.+)@(.+)$/;
     var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
     var validChars="\[^\\s" + specialChars + "\]";
     var quotedUser="(\"[^\"]*\")";
     var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
     var atom=validChars + '+';
     var word="(" + atom + "|" + quotedUser + ")";
     var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
     var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
     var matchArray=emailStr.match(emailPat);
     if (matchArray == null) {
         return false;
     }
     
     var user=matchArray[1];
     var domain=matchArray[2];
     if (user.match(userPat) == null) {
          return false;
     }
            
     var IPArray = domain.match(ipDomainPat);
     if (IPArray != null) {
        for (var i = 1; i <= 4; i++) {
             if (IPArray[i] > 255) {
                      return false;
             }
        }
        return true;
     }
     
     var domainArray=domain.match(domainPat);
     if (domainArray == null) {
          return false;
     }
     
     var atomPat=new RegExp(atom,"g");
     var domArr=domain.match(atomPat);
     var len=domArr.length;
     if ((domArr[domArr.length-1].length < 2) ||
         (domArr[domArr.length-1].length > 3)) {
                return false;
      }
      
      if (len < 2) {
         return false;
      }
   
     return true;
 }
 
/**
 * 验证电话号码是否合法
 * 
 * @param 要验证的电话号码
 * @return 布尔值
 */
CustomerValidator.isTelephone = function(telephoneStr){
	
	telephoneStr = CustomerValidator.removeLinkcharacter(telephoneStr);
    if (telephoneStr.length < 7 || telephoneStr.length > 60){
        return false;
    }
    
    var reg=/^[0-9]{7,60}$/;
    
    return reg.test(telephoneStr);
}

/**
 * 验证手机号码是否合法
 * 
 * @param 要验证的手机号码
 * @return 布尔值
 */
CustomerValidator.isHandSet = function(mobileStr){
 	if ( mobileStr.length < 11 || mobileStr.length > 20 ) {
        return false;
    }
    
    var reg=/^[0-9]{11,20}$/;
    
    return reg.test(mobileStr);
 }
 
 /**
  * 去除电话/或传真中的"-"字符
  */
CustomerValidator.removeLinkcharacter = function(str){
	return str.replace("-", "");
}



/**
 * 验证以“,”分隔的字符串是否重复
 * eg: 对于"test, pplive, test"，调用该方法将返回false
 * 
 * @param str
 */
CustomerValidator.isExistedDuplicatedStr = function(str){
	var strArray = str.split(',');
	for(var i = 0; i < strArray.length; i++){
		var str1 = strArray[i];
		for(var j = i+1; j < strArray.length; j++){
			if(str1 == strArray[j])
				return true;
		}
	}	
					
	return false;
}

/**
 * 验证字符串是否符合URL格式
 */
CustomerValidator.isUrl = function(str){
	var reg = /^http:\/\/.{0,93}/;
	return reg.test(str);
}

/**
 * 判断字符串中是否包含可执行代码
 */
CustomerValidator.isContainExecutableScript = function(str){
	return /<script/i.test(str) || /<\/script>/i.test(str);
}

CustomerValidator.isValidImageFilePath = function(str){
	return /^([a-zA-Z]:)((\\)[^\\\*\?\|\/:"\<>]{1,255})+\.(([j,J][p,P][g,G])|([g,G][i,I][f,F])|([p,P][n,N][g,G]))$/.test(str); 
	
}

/**
 * 判断是否为整数
 */
CustomerValidator.isNumber = function(num){
 	return /^[0-9]+$/.test(num);
}

/**
 * 判断身份证号码格式是否正确
 */
CustomerValidator.isTrueIDCard = function(value) {
	if(value.length != 15 && value.length != 18)
		return false;
		
	if(value.length == 15 && !CustomerValidator.isNumber(value)) {
		return false;
	}	
	
	var reg = /^[a-zA-Z0-9]+$/;
	if(value.length == 18 && !reg.test(value)) {
		return false;
	}
	
	return true;
};
	
CustomerValidator.isValidDecimal = function(value){
	var reg = /^[1-9][0-9]*\.?[0-9]*$/;
	if(!reg.test(value))
		return false;
	
	var pos = value.indexOf(".");	
	if(pos > -1 && pos == value.length - 1)
		return false;

	return true;
}
 